<?php
/**
 * GeryEngine
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this release in the file LICENSE
 *
 * @copyright  Copyright (c) 2007 Arjen Brouwer
 * @license    New BSD License
 * @author     Arjen Brouwer
 */

class GeryString extends GeryBaseType {

	public function ascii($val) {
		if (! strlen($val))
			return -1;
			
		return ord($val[0]);
	}
	
	public function downcase($val) {
		return strtolower($val);
	}
	
	public function escape($val) {
		return htmlspecialchars($val);
	}
	
	public function json($val) {
		return json_encode($val);
	}
	
	public function length($val) {
		return strlen($val);
	}
	
	public function lpad($val, $parameters) {
		if (! $this->expectParameters(__METHOD__, 'i|m', $parameters))
			return null;
		
		if (count($parameters) == 1) {
			return str_pad($val, $parameters[0], null, STR_PAD_LEFT);
		} else {
			return str_pad($val, $parameters[0], $parameters[1], STR_PAD_LEFT);
		}
	}
	
	public function replace($val, $parameters) {
		if (
			$this->expectParameters(__METHOD__, 'ss', $parameters, true)
			|| $this->expectParameters(__METHOD__, 'aa', $parameters, true)
			|| $this->expectParameters(__METHOD__, 'as', $parameters, true)	
		) {
			return str_replace($parameters[0], $parameters[1], $val);
		} else {
			$this->getLogger()->warning('Invalid arguments to ' . __METHOD__);
		}
	}
	
	public function reverse($val, $parameters) {
		return strrev($val);
	}
	
	public function split($val, $parameters) {
		if (! $this->expectParameters(__METHOD__, 's', $parameters))
			return false;
		
		return explode($parameters[0], $val);
	}
	
	public function substring($val, $parameters) {
		if (! $this->expectParameters(__METHOD__, 'i|i', $parameters))
			return null;
		
		if (count($parameters) == 1) {
			return substr($val, $parameters[0]);
		} else {
			return substr($val, $parameters[0], $parameters[1]);
		}
	}
	
	public function to_float($val) {
		return floatval($val);
	}
	
	public function to_int($val) {
		return intval($val);
	}
	
	public function trim($val, $parameters) {
		if ($this->expectParameters(__METHOD__, '|s', $parameters)) {
			if (! count($parameters))
				return trim($val);
			else 
				return trim($val, $parameters[0]);
		}
	}
	
	public function ucfirst($val) {
		return ucfirst($val);
	}
	
	public function ucwords($val) {
		return ucwords($val);
	}
	
	public function upcase($val) {
		return strtoupper($val);
	}
}
?>